/************************************************************************/
/* dxtmanager.cpp                                                       */
/* 2004/04/30                                                           */
/************************************************************************/
#include <d3dx9.h>
#include "DXTManager.h"
#include "global.h"
#include <ClientResourceManager.h>
#include <GameHelper.h>
/************************************************************************/
/*                                                                      */
/************************************************************************/
HRESULT
FX_UTIL::CreateCompressedTexture( LPDIRECT3DDEVICE9 pDevice,
                                 const char* szTextureFileName,
//                                 LPDIRECT3DTEXTURE9& pTextrue,
                                 TextureItem*& pTextrue,
                                 DWORD bIsSupportCompressedTexture )
    {

        pTextrue = 0;        
        if (!pDevice) { return E_FAIL; }

//        _D3DPOOL pool = g_Draw.DEVICE_POOL;
        _D3DPOOL pool = D3DPOOL_MANAGED;
        _D3DFORMAT fileformat = D3DFMT_UNKNOWN;

        if (!bIsSupportCompressedTexture)
        {
            pool = D3DPOOL_MANAGED;
            fileformat = D3DFMT_A1R5G5B5; 
        }
		char rename[256];
		memset(rename,0,256);
		int i=strlen(szTextureFileName)-1;
		for (;i>=0;i--)
		{
			if (szTextureFileName[i]=='\\'){i++;break;}
		}
		strncpy(rename,szTextureFileName+i,strlen(szTextureFileName)-i);
		char fullfilename[256];
		char lowcase[256];
		sprintf(fullfilename,"%s%s",GameHelper::GetRootFolder(),&szTextureFileName[2]);
		strlwr(fullfilename);


		pTextrue=ResourcePoolManager->CharacterTexturePool().findResource(fullfilename);
//		pTextrue=ResourcePoolManager->CharacterTexturePool().findResource(rename);
		if (pTextrue )return S_OK;
		if (!pTextrue )pTextrue = ResourcePoolManager->CharacterTexturePool().createResource(fullfilename);
//		if (!pTextrue )pTextrue = ResourcePoolManager->CharacterTexturePool().createResource(rename);
		if (!pTextrue )return -1;
		pTextrue->setTextureParam(D3DX_DEFAULT, D3DX_DEFAULT, 1, 0, fileformat,0,D3DX_FILTER_NONE,D3DX_FILTER_NONE , D3DPOOL_MANAGED);
//		pTextrue->loadResource(szTextureFileName);
//		pTextrue->loadResource(fullfilename);
/*
        HRESULT hr = S_OK;
        hr = D3DXCreateTextureFromFileEx(pDevice, szTextureFileName,
                                         D3DX_DEFAULT, D3DX_DEFAULT, 1,
                                         0, fileformat, pool,
//                                         D3DX_FILTER_TRIANGLE,
                                         D3DX_FILTER_NONE,
                                         D3DX_FILTER_NONE, 0x00,
                                         NULL, NULL, &pTextrue);

        if (FAILED(hr))
        {
#ifdef _DEBUG
            char szOutputString[128] = "";
            strcpy(szOutputString, szTextureFileName);
            strcat(szOutputString, " couldn't find.");
            OutputDebugString(szOutputString);
#endif _DEBUG
            return E_FAIL;
        }*/
        return S_OK;
    }